<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category\Category;
use App\Models\Order;
use App\Models\SystemConfig;
use App\Models\User;
use App\Services\CategoryService;
use App\Transformers\System\SystemSettingsTransformer;
use Illuminate\Http\Request;

class PromoterSetController extends Controller
{

    public $model = SystemConfig::class;

    public $transformer = SystemSettingsTransformer::class;

    public $categoryService;


    /**
     * 获取顶部标签
     */
    public function get_system_header(Request $request)
    {
        $this->categoryService  = new CategoryService(request(),'config');

        $data = Category::query()
            ->where('category_group_id', $this->categoryService->category_group_id)
            ->select('id', 'id as value', 'nickname', 'pid')
            ->orderBy('id', 'asc')
            ->pimp()
            ->get()
            ->toArray();

        // 生成树
        $tree = generateTree(objectToArray($data), 'value');

        $datas = [];
        foreach($tree as $value){
            if ($value['nickname'] == '分销配置' || $value['nickname'] == '积分配置'){
                $datas[] = $value;
            }
        }

        return $this->response->array($datas);
    }


    /**
     * 列表
     */
    public function index(Request $request)
    {

        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);

        $data = $this->getQuery()->pimp()->get();

        return $this->response->collection($data,new SystemSettingsTransformer());
    }

    /**
     * 修改
     */
    public function update(Request $request)
    {
        $this->validateData($request);

        $category_name = $request->input('category_name');

        $menu_name_value = $request->all();

        unset($menu_name_value['category_name']);

        $system_config_jihe = $this->getQuery()->where('category_name',$category_name)->get();

        foreach ($menu_name_value as $menu_name=>$value){
            $model = $system_config_jihe->where('menu_name',$menu_name)->first();
            $model->value = $value;
            $model->save();
        }

        return $this->response->noContent();
    }

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [];
                break;
            case 'PUT':
                return [];
                break;
            case 'PATCH':
                return [
                    'value' => 'bail|required',
                    'category_name' => 'bail|required',
                ];
                break;
        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'category_name.required' => '分类昵称 不能为空',
        ];
    }

}
